package com.baidu.sensitivewordfiltering.service.impl;

import com.baidu.sensitivewordfiltering.service.SensitiveWordService;
import com.baidu.sensitivewordfiltering.util.SensitiveWordInit;
import com.baidu.sensitivewordfiltering.util.SensitivewordEngine;
import com.baidu.sensitivewordfiltering.mapper.SensitiveWordDao;
import com.baidu.sensitivewordfiltering.po.SensitiveWord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;


@Service
public class SensitiveWordServiceImpl implements SensitiveWordService {
     @Autowired
    private SensitiveWordDao sensitiveWordDao;

    @Override
    public Set<String> sensitiveWordFiltering(String text)
    {
        // 初始化敏感词库对象
        SensitiveWordInit sensitiveWordInit = new SensitiveWordInit();
        // 从数据库中获取敏感词对象集合（调用的方法来自Dao层，此方法是service层的实现类）
        List<SensitiveWord> sensitiveWords = sensitiveWordDao.selectList(null);
        // 构建敏感词库
        Map sensitiveWordMap = sensitiveWordInit.initKeyWord(sensitiveWords);
        // 传入SensitivewordEngine类中的敏感词库
        SensitivewordEngine.sensitiveWordMap = sensitiveWordMap;
        // 得到敏感词有哪些，传入2表示获取所有敏感词
        Set<String> set = SensitivewordEngine.getSensitiveWord(text, 2);
        return set;
    }

    @Override
    public String replaceSensitiveWords(String text) {
        // 初始化敏感词库对象
        SensitiveWordInit sensitiveWordInit = new SensitiveWordInit();
        // 从数据库中获取敏感词对象集合（调用的方法来自Dao层，此方法是service层的实现类）
        List<SensitiveWord> sensitiveWords = sensitiveWordDao.selectList(null);
        // 构建敏感词库
        Map sensitiveWordMap = sensitiveWordInit.initKeyWord(sensitiveWords);
        // 传入SensitivewordEngine类中的敏感词库
        SensitivewordEngine.sensitiveWordMap = sensitiveWordMap;
        // 得到敏感词有哪些，传入2表示获取所有敏感词
        String strReplace = SensitivewordEngine.replaceSensitiveWord(text, 2,"*");
        return strReplace;
    }
}
